home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / speech / examples / colors.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.9 KB  |  253 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18. This is a very simple speech demo.  A large X window opens and changes colors
  19. among red, green, blue, yellow, black, & white when the color name is spoken.
  20.  
  21. The templates for color names should be placed in
  22. /usr/share/data/speech/templates/SpeakColors
  23.  
  24. Stephen Junkins, March 12, 1994.
  25. Virtual Reality Systems Laboratory
  26. Computer Science Department
  27. Clemson University
  28. Clemson, South Carolina
  29. */
  30.  
  31. #include    <stdio.h>
  32. #include    <assert.h>
  33. #include    <X11/Xlib.h>
  34. #include    <X11/Xresource.h>
  35. #include    <speech/Recognizer.h>
  36. #include    <speech/Word.h>
  37. #include    <speech/WordCallbackBindings.h>
  38. #include    <speech/Vocabulary.h>
  39. #include    <speech/Condition.h>
  40. #include    <speech/Event.h>
  41. #include    <speech/Error.h>
  42. #include    <speech/Shorthand.h>        /* required for C API */
  43.  
  44. static const int X = 100 ;
  45. static const int Y = 100 ;
  46. static const int W = 800 ;
  47. static const int H = 800 ;
  48.  
  49. #define RED         1 
  50. #define COMBO_RG    2 
  51. #    define YELLOW    2 
  52. #define GREEN       3
  53. #define COMBO_GB    4 
  54. #    define CYAN    4 
  55. #define BLUE        5
  56. #define COMBO_RB    6 
  57. #    define MAGENTA    6 
  58. #define BLACK       7
  59. #define WHITE       8
  60.  
  61.  
  62. /* Declarations */
  63. char hello[] = {"Demo of SGI Speech Software using C API"};
  64.  
  65. long create_color(
  66.         Display* display, Colormap colormap,
  67.         unsigned short red, unsigned short green, unsigned short blue )
  68. {
  69.     XColor     tmpColor;
  70.     
  71.     tmpColor.red = red;
  72.     tmpColor.green = green;
  73.     tmpColor.blue = blue;
  74.  
  75.     if(XAllocColor(display, colormap, &tmpColor) == 0)
  76.         fprintf(stderr,"Unable to allocate color.\n");
  77.  
  78.     return tmpColor.pixel ;
  79.     }
  80.  
  81. create_colors(display, window, colors)
  82.     Display *display;
  83.     Window   window;
  84.     long    *colors;
  85. {
  86.     Colormap   colormap;
  87.     int        screen;
  88.  
  89.     screen = DefaultScreen (display);
  90.  
  91.     /* Create an X colormap structure: */
  92.     colormap = XCreateColormap(display, window,
  93.                DefaultVisual(display, screen), AllocNone);
  94.     XSetWindowColormap(display, window, colormap);
  95.  
  96.     colors[RED] = create_color( display, colormap, 65530, 0, 0 ) ;
  97.     colors[COMBO_RG] = create_color( display, colormap, 65530, 65530, 0 ) ;
  98.     colors[GREEN] = create_color( display, colormap, 0, 65530, 0 ) ;
  99.     colors[COMBO_GB] = create_color( display, colormap, 0, 65530, 65530 ) ;
  100.     colors[BLUE] = create_color( display, colormap, 0, 0, 65530 ) ;
  101.     colors[COMBO_RB] = create_color( display, colormap, 65530, 0, 65530 ) ;
  102.     colors[BLACK] = create_color( display, colormap, 0, 0, 0 ) ;
  103.     colors[WHITE] = create_color( display, colormap, 65530, 65530, 65530 ) ;
  104. }
  105.  
  106.  
  107. /** Globals: */
  108. long        colors[256];
  109. Window     window ;
  110. Display *display;
  111. GC            colorgc;
  112.  
  113.  
  114. void setColor( const Event* event, void* v )
  115.     {
  116.     XSetForeground (display, colorgc, colors[(int)v]);
  117.     XFillRectangle(display, window, colorgc, 0, 0, W, H);
  118.     }
  119.  
  120. void quit( const Event* event, void* v )
  121.     {
  122.     exit(0);
  123.     }
  124.  
  125. void usage( const char* const progName )
  126.     {
  127.     fprintf( stderr, "usage <word1> <word2>\n" ) ;
  128.     fflush( stderr ) ;
  129.     }
  130.  
  131. int main( int argc, char* argv[] )
  132. {
  133.     XEvent event ;
  134.  
  135.     Vocabulary* localWords ;
  136.     Vocabulary* globalWords ;
  137.  
  138.     unsigned char swapConditionsUsingVocabularies = 0 ;
  139.  
  140.     char* applicationClass = "SpeakColors" ;
  141.     char* applicationName = argv[0] ;
  142.  
  143.     char fileName[256] ;
  144.     XrmDatabase xrmDatabase    ;
  145.     unsigned long valuemask;
  146.     XVisualInfo      *visual, gob;
  147.     XSetWindowAttributes attrib;
  148.     int count;
  149.  
  150.     Recognizer* recognizer ;
  151.  
  152.     sprintf( fileName, "%s/.Xdefaults", getenv( "HOME" ) ) ;
  153.     xrmDatabase    = XrmGetFileDatabase( fileName ) ; /* toolkit-dep */
  154.  
  155.     /** Create a recognizer: **/
  156.     recognizer = Recognizer_new(
  157.             applicationClass, applicationName, xrmDatabase,
  158.             "",     /* default display, program name */
  159.             Recognizer_RecognitionInterest()
  160.                     /* | Recognizer_RejectionInterest() */
  161.                     /* | Recognizer_AmbiguousInterest() */
  162.                     /* | Recognizer_ErrorInterest() */ ) ;
  163.     assert( Recognizer_status( recognizer ) == Error_OK ) ;
  164.     display =  Recognizer_dpy( recognizer );
  165.     XSpeechSetEnable (display, SpeechEnableNormal);
  166.  
  167.     /* words with global focus */
  168.     {
  169.     ActionCallbackBinding globalActionFunctionBindings[] = {
  170.         "quit_colors", quit, (void*)0,
  171.         (char*)0, (CallbackFunctionPointer)0, (void*)0,
  172.         } ;
  173.  
  174.     Condition* globalCondition = Condition_newGlobal() ;
  175.     Recognizer_newWords( recognizer, globalActionFunctionBindings,
  176.             globalCondition, &globalWords ) ;
  177.     if( Recognizer_status( recognizer ) != Error_OK )
  178.         {
  179.         fprintf( stderr, "trouble loading global words\n" ) ;
  180.         fflush( stderr ) ;
  181.         }
  182.     Condition_delete( globalCondition ) ;
  183.     }
  184.  
  185.  
  186.     /* words with window(local) focus */
  187.     {
  188.     ActionCallbackBinding localActionFunctionBindings[] = {
  189.         "red",   setColor, (void*)RED,
  190.         "green", setColor, (void*)GREEN,
  191.         "blue",  setColor, (void*)BLUE,
  192.         "yellow",  setColor, (void*)YELLOW,
  193.         "cyan",  setColor, (void*)CYAN,
  194.         "magenta",  setColor, (void*)MAGENTA,
  195.         "white",  setColor, (void*)WHITE,
  196.         "black",  setColor, (void*)BLACK,
  197.         "quit",  quit, (void*)0,
  198.         (char*)0, (CallbackFunctionPointer)0, (void*)0,
  199.         } ;
  200.  
  201.     /**
  202.     Create a Window (need win for specification of following local words):
  203.     **/
  204.     int screen = DefaultScreen( Recognizer_dpy( recognizer ) ) ;
  205.     Window root = RootWindow( Recognizer_dpy( recognizer ), screen ) ;
  206.  
  207.     long white = WhitePixel(Recognizer_dpy(recognizer), screen ) ;
  208.     long black = BlackPixel(Recognizer_dpy(recognizer), screen ) ;
  209.     window = XCreateSimpleWindow( Recognizer_dpy( recognizer ), root,
  210.             X, Y, W, H, 1, white, black ) ;
  211.     XMapWindow( Recognizer_dpy( recognizer ), window ) ;
  212.  
  213.     /* GC creation and initialization */
  214.     colorgc = XCreateGC (display, window, 0, 0);
  215.     create_colors (display, window, colors);
  216.  
  217.  
  218.     {
  219.     Condition* localCondition = Condition_newWindow( window ) ;
  220.     Recognizer_newWords( recognizer, localActionFunctionBindings,
  221.             localCondition, &localWords ) ;
  222.     if( Recognizer_status( recognizer ) != Error_OK )
  223.         {
  224.         fprintf( stderr, "trouble loading local words\n" ) ;
  225.         fflush( stderr ) ;
  226.         }
  227.     Condition_delete( localCondition ) ;
  228.     }
  229.     }
  230.  
  231.  
  232.     /* toolkit-dependent modification here */
  233.     do
  234.         {
  235.         XNextEvent( Recognizer_dpy( recognizer ), &event ) ;
  236.  
  237.         if( Recognizer_isSpeechEvent( recognizer, &event ) )
  238.             Recognizer_processEvent( recognizer, &event ) ;
  239.          else
  240.             ; /* process as other/normal event */
  241.  
  242.         } while( event.type != DestroyNotify ) ;
  243.  
  244.     XrmDestroyDatabase( xrmDatabase ) ;
  245.  
  246.     {
  247.     Error retVal = Recognizer_status( recognizer ) ;
  248.     Recognizer_delete( recognizer ) ;
  249.     return retVal ;
  250.     }
  251.     }
  252.  
  253.